package com.baidu.threadPoolExcutor;

import java.util.concurrent.*;

public class ApplicationPool {
    public static void main(String[] args) {
        //1.创建线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                //1.常驻线程数
                3,
                //2.最大线程数
                5,
                //3.临时线程存活时间
                10,
                //4.临时线程存活时间的单位
                TimeUnit.SECONDS,
                //5.队列大小
                new ArrayBlockingQueue<>(3),
                //6.线程工厂
                Executors.defaultThreadFactory(),
                //7.若线程及队列全满 新线程来的处理方式
                new ThreadPoolExecutor.AbortPolicy()
        );

        //2.创建线程对象 并放到线程池处理
        MyThread myThread = new MyThread();

        //3.放入池中
        pool.execute(myThread);
        pool.execute(myThread);

        //4.创建runnable线程对象 放到池中处理
        MyRunnable myRunnable = new MyRunnable();

        //5.放入池中 这里需要使用submit方法
        pool.submit(myRunnable);
        pool.submit(myRunnable);

        //6.创建Callable线程对象
        Future<String> submit1 = pool.submit(new MyCallable(5));
        Future<String> submit2 = pool.submit(new MyCallable(10));
        Future<String> submit3 = pool.submit(new MyCallable(15));
        Future<String> submit4 = pool.submit(new MyCallable(20));
        Future<String> submit5 = pool.submit(new MyCallable(25));

        try {
            System.out.println(submit1.get());
            System.out.println(submit2.get());
            System.out.println(submit3.get());
            System.out.println(submit4.get());
            System.out.println(submit5.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
